<?php
namespace admin;
use models\BaseDao;
use Slince\Upload\UploadHandlerBuilder;
class Setting extends Admin{
    public function  __construct(){
        parent::__construct();
        $this->assign('menumark','setting');
    }
/**
 * 基本设置列表页面
 */
    function index(){
        // 获取数据库操作对象
        $db = new BaseDao();
        // 获取全部基本设置,按照ord排序
        $data = $db->select('setting','*');
        // 将数据全部分给模板
        foreach($data as $v){
            $this->assign($v['skey'],$v['svalue']);
        }

        //标题    
        $this->assign('title','基本设置列表');
        // 导入模板
        $this->display('setting/mod');
    }
  
    function doupdate(){


        if($_FILES['web_logo']['error']==0){
            // 修改图片
            $path = TEMPDIR."/uploads";
            $builder = new UploadHandlerBuilder(); //create a builder.
            $handler = $builder
            //add constraints
            ->allowExtensions(['jpg','png','gif'])
            ->allowMimeTypes(['image/*'])

            ->saveTo($path) //save to local
            ->getHandler();

            $files = $handler->handle();
            $filename = $files['web_logo']->getUploadedFile()->getClientOriginalName(); // original name

            $newfilename = date('Y-md') . '-' . uniqid() . '.' . $files['web_logo']->getUploadedFile()->getClientOriginalExtension();

            rename($path.'/'.$filename,$path.'/'.$newfilename);
            $_POST['web_logo'] = $newfilename;
        }
        $db = new BaseDao();
        $num = 0;
        foreach($_POST as $k => $v){
            $num += $db->update('setting',['svalue'=>$v],['skey'=>$k]);
        }
        if($num){
            $this->success('/admin/setting','基本信息修改ok');
        }else{
            $this->error('/admin/setting','基本信息修改fail');
        }
    }

}